/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ultrasoft.farmasoft.domain;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;


import com.ultrasoft.farmasoft.domain.Package;

/**
 *
 * @author Administrador						  
 */
@Entity
@Table(name = "item")			
@XmlRootElement			
@NamedQueries({
    @NamedQuery(name = "Item.findAll", query = "SELECT i FROM Item i"),
    @NamedQuery(name = "Item.findByPackageId", query = "SELECT i FROM Item i WHERE i.itemPK.packageId = :packageId"),
    @NamedQuery(name = "Item.findByPackageIdByState", query = "SELECT i FROM Item i WHERE i.itemPK.packageId = :packageId AND i.state = :state"),
    @NamedQuery(name = "Item.findByPackageIdByItemId", query = "SELECT i FROM Item i WHERE i.itemPK.packageId = :packageId AND i.itemPK.itemId = :itemId"),
    @NamedQuery(name = "Item.findByItemId", query = "SELECT i FROM Item i WHERE i.itemPK.itemId = :itemId"),
    @NamedQuery(name = "Item.findByStockId", query = "SELECT i FROM Item i WHERE i.stockId.stockId = :stockId"),
    @NamedQuery(name = "Item.findByStockIdByProviderId", query = "SELECT i FROM Item i WHERE i.stockId.stockId = :stockId AND i.package1.pharmacyproduct.pharmacyproductPK.providerId = :providerId"),
    @NamedQuery(name = "Item.findByPharmacyId", query = "SELECT i FROM Item i WHERE i.stockId.userpharmacy.userpharmacyPK.pharmacyId = :pharmacyId"),
    @NamedQuery(name = "Item.findByPharmacyIdByStockId", query = "SELECT i FROM Item i WHERE i.stockId.userpharmacy.userpharmacyPK.pharmacyId = :pharmacyId AND i.stockId.stockId = :stockId"),
    @NamedQuery(name = "Item.findByPharmacyIdByStockIdByPackageId", query = "SELECT i FROM Item i WHERE i.stockId.userpharmacy.userpharmacyPK.pharmacyId = :pharmacyId AND i.stockId.stockId = :stockId AND i.itemPK.packageId = :packageId"),
    @NamedQuery(name = "Item.findByStockIdByPackageId", query = "SELECT i FROM Item i WHERE i.stockId.stockId = :stockId AND i.itemPK.packageId = :packageId"),
    @NamedQuery(name = "Item.findByStockIdByPackageIdByState", query = "SELECT i FROM Item i WHERE i.stockId.stockId = :stockId AND i.itemPK.packageId = :packageId AND i.state = :state"),
    @NamedQuery(name = "Item.findByCost", query = "SELECT i FROM Item i WHERE i.cost = :cost"),
    @NamedQuery(name = "Item.findByState", query = "SELECT i FROM Item i WHERE i.state = :state"),
    @NamedQuery(name = "Item.findByItemIdMax", query = "SELECT max(i.itemPK.itemId) FROM Item i WHERE i.itemPK.packageId = :packageId")})
public class Item implements Serializable {
	private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected ItemPK itemPK;
    @Basic(optional = false)
    @Column(name = "cost")
    private double cost;
    @Basic(optional = false)
    @Column(name = "state")
    private short state;    
    @Column(name = "tempState")
    private short tempState;
	@OneToMany(mappedBy = "item")
    private Collection<Decline> declineCollection;
    @OneToMany(mappedBy = "item")
    private Collection<Transfer> transferCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "item")
    private Collection<Credititem> credititemCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "item")
    private Collection<Credititempharmacy> credititempharmacyCollection;
    @JoinColumn(name = "packageId", referencedColumnName = "packageId", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Package package1;
    @JoinColumn(name = "stockId", referencedColumnName = "stockId")
    @ManyToOne
    private Stock stockId;

    public Item() {
    }

    public Item(ItemPK itemPK) {
        this.itemPK = itemPK;
    }

    public Item(ItemPK itemPK, double cost, short state) {
        this.itemPK = itemPK;
        this.cost = cost;
        this.state = state;
    }

    public Item(int packageId, int itemId) {
        this.itemPK = new ItemPK(packageId, itemId);
    }
    
    public Item(Package packageTemp, int itemId) {
        this.itemPK = new ItemPK(packageTemp.getPackageId(), itemId);
    }
    
    public Item(Item item) {
        this.itemPK = new ItemPK(item.getItemPK().getPackageId(),item.getItemPK().getItemId());
    }

    public ItemPK getItemPK() {
        return itemPK;
    }

    public void setItemPK(ItemPK itemPK) {
        this.itemPK = itemPK;
    }

    public double getCost() {
        return cost;
    }

    public void setCost(double cost) {
        this.cost = cost;
    }

    public short getState() {
        return state;
    }

    public void setState(short state) {
        this.state = state;
    }

    public short getTempState() {
		return tempState;
	}

	public void setTempState(short tempState) {
		this.tempState = tempState;
	}
	
    @XmlTransient
    public Collection<Decline> getDeclineCollection() {
        return declineCollection;
    }

    public void setDeclineCollection(Collection<Decline> declineCollection) {
        this.declineCollection = declineCollection;
    }

    @XmlTransient
    public Collection<Transfer> getTransferCollection() {
        return transferCollection;
    }

    public void setTransferCollection(Collection<Transfer> transferCollection) {
        this.transferCollection = transferCollection;
    }

    @XmlTransient
    public Collection<Credititem> getCredititemCollection() {
        return credititemCollection;
    }

    public void setCredititemCollection(Collection<Credititem> credititemCollection) {
        this.credititemCollection = credititemCollection;
    }

    @XmlTransient
    public Collection<Credititempharmacy> getCredititempharmacyCollection() {
        return credititempharmacyCollection;
    }

    public void setCredititempharmacyCollection(Collection<Credititempharmacy> credititempharmacyCollection) {
        this.credititempharmacyCollection = credititempharmacyCollection;
    }

    public Package getPackage1() {
        return package1;
    }

    public void setPackage1(Package package1) {
        this.package1 = package1;
    }

    public Stock getStockId() {
        return stockId;
    }

    public void setStockId(Stock stockId) {
        this.stockId = stockId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (itemPK != null ? itemPK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Item)) {
            return false;
        }
        Item other = (Item) object;
        if ((this.itemPK == null && other.itemPK != null) || (this.itemPK != null && !this.itemPK.equals(other.itemPK))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.ultrasoft.farmasoft.domain.Item[ itemPK=" + itemPK + " ]";
    }
    
}
